const { location, history } = window

const pushState = (path, title = '精细化平台') => {
  history.pushState(null, title, path)
  if (!window.isQianKun) location.reload()
}

const replaceState = (path, title = '精细化平台') => {
  history.replaceState(null, title, path)
  if (!window.isQianKun) location.reload()
}

const getQuery = (key, url = location.href) => {
  // eslint-disable-next-line no-useless-escape
  const name = key.replace(/[\[\]]/g, '\\$&')
  const regex = new RegExp(`[?&]${name}(=([^&#]*)|&|#|$)`)
  const results = regex.exec(url)
  if (!results) return null
  if (!results[2]) return ''
  return decodeURIComponent(results[2].replace(/\+/g, ' '))
}

export default {
  getQuery,
  replaceState,
  pushState,
}
